package View;

//finish account info page
//buttons on item detail
//fix favorites tab
//
//change login password field to a password field
//
//put account info button on menu page
//put menu button on account info page
//
//fix logout button - done, probably
//make packages and organize classes - done
//javadocs
//make menu with pictures - menu made
//many reviews

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.BevelBorder;

import Controller.DB;
import Model.Customer;

import java.awt.FlowLayout;

/**
 * @author jrsto674
 * @version draft
 */
@SuppressWarnings("serial")
public class RestUWantGUI extends JFrame {

	private JPanel contentPane;
	
	private JPanel currentPanel;

	private LoginPanel myLoginPanel; 
	
	private MenuPanel myMenuPanel;
	
	private OrderPanel myOrderPanel;
	
	private RegisterPanel myRegisterPanel;
	
	private CompletePanel myCompletePanel;
	
	protected DB database;
	
	private Customer customer;
	
	private JLabel lblHelloUser;
	
	private JButton btnLogout;
	
	private JButton btnAccount;
	
	private JButton btnMenu;

	/**
	 * Constructor of GUI.
	 * @author jrsto674
	 * @throws IOException 
	 * @throws SQLException 
	 */
	public RestUWantGUI() throws SQLException, IOException {
		customer = new Customer();
		database = new DB();
		myLoginPanel = new LoginPanel(this);
		myOrderPanel = new OrderPanel(this);
		myRegisterPanel = new RegisterPanel(this);
		
		addWindowListener(new WindowAdapter() {
		      public void windowClosing(WindowEvent e) {
		        try {
					database.conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		      }
		    });
		
		buildGUI();
		
	}
	
	/**
	 * Swaps current panel to Register Panel.
	 * @author jrsto674
	 */
	public void toRegister() {
		currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(myRegisterPanel);
		currentPanel.revalidate();
	}
	
	/**
	 * Swaps current panel to Login Panel.
	 * @author jrsto674
	 */
	public void toLogin() {
		btnLogout.setVisible(false);
		btnAccount.setVisible(false);
		btnMenu.setVisible(false);
		updateUsername("New User");
		myLoginPanel = new LoginPanel(this);
		currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(myLoginPanel);
		currentPanel.revalidate();
		currentPanel.repaint();
	}
	
	/**
	 * Swaps from Login Panel to Menu Panel.
	 * @author jrsto674
	 * @throws SQLException 
	 */
	public void loginToMenu(Customer cus) throws SQLException {
		btnLogout.setVisible(true);
		btnAccount.setVisible(true);
		btnMenu.setVisible(true);
		btnAccount.setEnabled(true);
		btnMenu.setEnabled(false);
		customer = cus;
		myMenuPanel = new MenuPanel(this, database, customer);
		currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(myMenuPanel);
		currentPanel.revalidate();
		currentPanel.repaint();
	}
	
	/**
	 * Swaps current panel to Account Info Panel.
	 * @author alpertmd
	 */
	public void toAccountInfo() {
		btnMenu.setEnabled(true);
		btnAccount.setEnabled(false);
		currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(new AccountInfoPanel(this, customer, database));
		currentPanel.revalidate();
		currentPanel.repaint();
		
	}
	
	/**
	 * Swaps current panel to Menu Panel.
	 * @author jrsto674
	 * @throws SQLException 
	 */
	public void toMenu() throws SQLException {
		btnMenu.setEnabled(false);
		btnAccount.setEnabled(true);
		currentPanel.remove(currentPanel.getComponent(0));
		currentPanel.add(new MenuPanel(this, database, customer));
		currentPanel.revalidate();
		currentPanel.repaint();
	}	
	

	
	/**
	 * Swaps from Order Confirmation Panel to Order Complete Panel.
	 */
	public void orderToComplete() {
		btnMenu.setEnabled(true);
		btnAccount.setEnabled(true);
		currentPanel.remove(currentPanel.getComponent(0));
		myCompletePanel = new CompletePanel(this, customer.getOrder());
		currentPanel.add(myCompletePanel);
		currentPanel.revalidate();
		currentPanel.repaint();
	}
	
	/**
	 * This method updates the welcome message in the
	 * upper right of the GUI.
	 * @author jrsto674
	 */
	public void updateUsername(String username) {
		lblHelloUser.setText(username);
		lblHelloUser.revalidate(); 
		repaint();
	}	
	
	public void resetOrder() {
		customer.resetOrder();
	}
	/**
	 * This builds the GUI frame
	 * @author jrsto674
	 * @throws IOException 
	 */
	private void buildGUI() throws IOException {
		
		JPanel panelHead = new JPanel();
		panelHead.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 0), new Color(128, 0, 128), new Color(255, 255, 0), new Color(255, 255, 0)));
		
		currentPanel = new JPanel();
		currentPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JPanel panelUserInfo = new JPanel();
		panelUserInfo.setBorder(new LineBorder(new Color(0, 0, 0)));
		currentPanel.setLayout(new BorderLayout(5, 5));
				
		currentPanel.add(myLoginPanel); // Initial Login Panel
		currentPanel.setAlignmentX(CENTER_ALIGNMENT);
		currentPanel.setAlignmentY(CENTER_ALIGNMENT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 550);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panelLogo = new JPanel();
        btnAccount = new JButton("AccountInfo");
		btnAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				toAccountInfo();
			}
		});
		
		btnMenu = new JButton("Menu");
		btnMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					toMenu();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		panelLogo.add(btnAccount);
		panelLogo.add(btnMenu);
		getContentPane().add(panelLogo);
		
		btnAccount.setVisible(false);
		btnMenu.setVisible(false);
		
		lblHelloUser = new JLabel("New User");

		JLabel lblNewLabel = new JLabel();
		lblNewLabel.setIcon(new ImageIcon("logo.png"));
		
		// THIS THE GROUP LAYOUT FOR THE GUIS
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panelLogo, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelHead, GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelUserInfo, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED))
				.addComponent(currentPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panelHead, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelUserInfo, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelLogo, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
					.addGap(8)
					.addComponent(currentPanel, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
		);
		
		
		btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				customer = null;
				toLogin();
			}
		});
		
		btnLogout.setVisible(false);
		panelUserInfo.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panelUserInfo.add(lblHelloUser);
		panelUserInfo.add(btnLogout);
		
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 40));
		GroupLayout gl_panelHead = new GroupLayout(panelHead);
		gl_panelHead.setHorizontalGroup(
			gl_panelHead.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelHead.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panelHead.setVerticalGroup(
			gl_panelHead.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panelHead.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelHead.setLayout(gl_panelHead);
		
		contentPane.setLayout(gl_contentPane);
		
	}
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RestUWantGUI frame = new RestUWantGUI();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
